package com.xbongbong.saas.service.impl;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.service.CompanyConfigService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.contractperformance.pojo.ContractPerformanceLogPojo;
import com.xbongbong.pro.contractperformance.pojo.dto.ContractPerformanceDeleteBatchDTO;
import com.xbongbong.pro.contractperformance.pojo.vo.ContractPerformanceDeleteBatchVO;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.constant.ParameterConstant;
import com.xbongbong.saas.domain.entity.ContractPerformanceEntity;
import com.xbongbong.saas.help.workflow.ContractPerformanceHelp;
import com.xbongbong.saas.model.ContractPerformanceModel;
import com.xbongbong.saas.service.ContractPerformanceService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author 吴峰
 * @version v1.0
 * @date 2019/2/27 21:30
 * @since v1.0
 */
@Service("contractPerformanceService")
public class ContractPerformanceServiceImpl implements ContractPerformanceService {

    private static final Logger LOG = LoggerFactory.getLogger(ContractPerformanceServiceImpl.class);

    @Resource
    private ContractPerformanceModel contractPerformanceModel;
    @Resource
    private CompanyConfigService companyConfigService;
    @Resource
    private ContractPerformanceHelp contractPerformanceHelp;

    @Override
    public ContractPerformanceDeleteBatchVO deleteBatch(ContractPerformanceDeleteBatchDTO contractPerformanceDeleteBatchDTO) throws XbbException {
        ContractPerformanceDeleteBatchVO contractPerformanceDeleteBatchVO = new ContractPerformanceDeleteBatchVO();
        try {
            List<Long> contractIdIn = contractPerformanceDeleteBatchDTO.getContractIdIn();
            String corpid = contractPerformanceDeleteBatchDTO.getCorpid();
            contractPerformanceModel.deleteByContractIdIn(contractIdIn, corpid);
        } catch (Exception e) {
            LOG.error("ContractPerformanceServiceImpl.deleteBatch 执行出错：", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        return contractPerformanceDeleteBatchVO;
    }

    @Override
    public Boolean openDistribution(String corpid) throws XbbException {
        return contractPerformanceModel.openDistribution(corpid);
    }

    /**
     * 在新建、编辑时根据签订人改变业绩归属，若没有业绩分配创建一条当前签订人的100%的业绩分配，若签订人改变，当前签订人替换原签订人的业绩分配
     *
     * @param newPaasFormDataEntity 合同实体
     * @throws XbbException 异常
     */
    @Override
    public void changeContractPerformance(PaasFormDataEntity newPaasFormDataEntity, List<ContractPerformanceLogPojo> contractPerformanceLogList) throws XbbException {
        contractPerformanceHelp.changeContractPerformance(newPaasFormDataEntity.getCorpid(), newPaasFormDataEntity.getId(), newPaasFormDataEntity.getData(), contractPerformanceLogList);
    }

    @Override
    public void updateRollbackByContractId(List<Long> contractIdIn, String corpid) throws XbbException {
        if (Objects.isNull(contractIdIn) || contractIdIn.isEmpty()) {
            return;
        }
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put(ParameterConstant.CORPID, corpid);
        param.put("contractIdIn", contractIdIn);
        List<ContractPerformanceEntity> performanceEntityList = contractPerformanceModel.findEntitys(param);
        if (Objects.nonNull(performanceEntityList) && !performanceEntityList.isEmpty()) {
            contractPerformanceModel.reductionEsInUpdate(performanceEntityList);
        }
    }
}
